package org.nViron.Graffiti;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import instar.dg.GraffitiCache.GraffitiCacheListener;
import instar.dg.Graffito.GraffitoBody;
import instar.dg.Graffito.GraffitoHeader;

public class GraffitiManager implements GraffitiCacheListener, Serializable {

    private List<GraffitoHeader> graffiti;
    private GraffitiSource GraffitiSource;
    private boolean dirty = true;

    public GraffitiManager(GraffitiSource manager) {
        graffiti = new ArrayList<GraffitoHeader>();
        this.GraffitiSource = manager;
    }

    public List<GraffitoHeader> getGraffitoHeaderAsList() {
        return graffiti;
    }

    @Override
    public void addGraffito(GraffitoHeader gh) {
        graffiti.add(gh);
        dirty = true;
        GraffitiSource.addGraffito(gh);
    }

    @Override
    public void popupGraffito(GraffitoHeader g) {
    }

    @Override
    public void removeGraffito(GraffitoHeader gh, boolean deleted) {
        if (gh != null) {
            graffiti.remove(gh);
            dirty = true;
            GraffitiSource.remove(gh.getId());
        }
    }

    @Override
    public void updateGraffito(GraffitoHeader gh) {
        if (gh != null) {
            graffiti.remove(gh);
            GraffitiSource.remove(gh.getId());
        }

        graffiti.add(gh);
        dirty = true;
        GraffitiSource.addGraffito(gh);
    }

    @Override
    public void bodyLoaded(GraffitoBody g) {
    }

    public void SetDirty(boolean bool) {
        dirty = bool;
    }

    public boolean isDirty() {
        return dirty;
    }
}
